package win.lioil.bluetooth.util;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.os.Build;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.content.Context;

import androidx.annotation.NonNull;

import java.sql.Timestamp;

import win.lioil.bluetooth.APP;

public class ECGCustomView extends View {
    public ECGCustomView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }


    Path path = new Path();
    private float DisplayScale = 1.f;
    private int mWidth = 0;
    private int mHeight = 0;
    @Override
    protected void onDraw(@NonNull Canvas canvas) {
        super.onDraw(canvas);

        Paint paint = new Paint();
        //去锯齿
        //paint.setAntiAlias(true);
        //设置颜色
        paint.setColor(getResources().getColor(android.R.color.holo_blue_light));
        //绘制普通圆
//        canvas.drawCircle(200,200,100,paint);
        //设置空心Style
        paint.setStyle(Paint.Style.STROKE);
        //设置空心边框的宽度
        paint.setStrokeWidth(20);
        //绘制空心圆
//        canvas.drawCircle(200,500,90,paint);

        //Log.i("[Time]", String.valueOf(System.currentTimeMillis()));
        showECG(path);
        //Log.i("[Time]", String.valueOf(System.currentTimeMillis()));

        canvas.drawPath(path, paint);

        path.reset();
        //Log.i("[Time]", String.valueOf(System.currentTimeMillis()));
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        mWidth = right - left + 1;
        mHeight = bottom - top + 1;
    }

    public void showChannel(Path path, float[] data, int len, int left, int top , int width, int height){
        int DataStart = (int) (APP.FillIndex - width * DisplayScale);
        if (DataStart < 0)
            DataStart = 0;

        //Log.i("[Size]", String.format("(%d %d) to (%d %d)", left, top, width, height));

        for (int n = 0 ; n < width - 1; ++n){
            int DataIndex = (int)(n * DisplayScale + DataStart);
            if (DataIndex >= APP.FillIndex - 1)
                break;

            if (n == 0)
                path.moveTo(left + n, top + data[DataIndex] + height / 2);

            path.lineTo(left + n + 1, top + data[DataIndex + 1] + height / 2);

            //Log.i("[POS]", String.format("(%d %f) to (%d %f)", n, data[DataIndex], n + 1, data[DataIndex + 1]));
        }
    }

    public void showECG(Path path){
        int Margin = 10;
        int ChannelWidth = mWidth - Margin * 2;
        int ChannelHeight = (mHeight - Margin * 7) / 6;

        int Y = Margin;
        showChannel(path, APP.I, 0, Margin, Y, ChannelWidth, ChannelHeight);
        Y += ChannelHeight + Margin;
        showChannel(path, APP.II, 0, Margin, Y, ChannelWidth, ChannelHeight);
        Y += ChannelHeight + Margin;
        showChannel(path, APP.III, 0, Margin, Y, ChannelWidth, ChannelHeight);
        Y += ChannelHeight + Margin;
        showChannel(path, APP.AVL, 0, Margin, Y, ChannelWidth, ChannelHeight);
        Y += ChannelHeight + Margin;
        showChannel(path, APP.AVR, 0, Margin, Y, ChannelWidth, ChannelHeight);
        Y += ChannelHeight + Margin;
        showChannel(path, APP.AVF, 0, Margin, Y, ChannelWidth, ChannelHeight);
    }
}
